package io.renren.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

import io.renren.dao.TPurviewUsermanageDao;
import io.renren.entity.SysUserEntity;
import io.renren.entity.TPurviewCustinfoEntity;
import io.renren.entity.TPurviewUsermanageEntity;
import io.renren.entity.User;
import io.renren.service.TPurviewCustinfoService;
import io.renren.service.TPurviewUsermanageService;
import io.renren.utils.ShiroUtils;
import io.renren.utils.httpclient.BatchIds;
import io.renren.utils.httpclient.BizException;
import io.renren.utils.httpclient.HttpRequestUtils;
import io.renren.utils.httpclient.IdParam;
import io.renren.utils.httpclient.MD5;
import io.renren.utils.httpclient.UUIDUitl;
import net.sf.json.JSONObject;



@Service("tPurviewUsermanageService")
@Transactional(rollbackFor=Throwable.class)
public class TPurviewUsermanageServiceImpl implements TPurviewUsermanageService {
	@Autowired
	private TPurviewUsermanageDao tPurviewUsermanageDao;
	@Autowired
	private TPurviewCustinfoService tPurviewCustinfoService;
	private static String  ipconfig = "ycwip.properties";
	@Override
	public TPurviewUsermanageEntity queryObject(String id){
		return tPurviewUsermanageDao.queryObject(id);
	}
	
	@Override
	public List<TPurviewUsermanageEntity> queryList(Map<String, Object> map){
		return tPurviewUsermanageDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return tPurviewUsermanageDao.queryTotal(map);
	}
	
	@Override
	public void save(TPurviewUsermanageEntity tPurviewUsermanage){
		String  isadmin = tPurviewUsermanage.getIsadmin();
		if(!"1".equals(isadmin)) {
		JSONObject  result=HttpRequestUtils.pinkUrl(getUrlIp(), "user/save", "add", changeToUser(tPurviewUsermanage));
		String  bool = (String) result.get("status");
		JSONObject user = result.getJSONObject("data");
		String userid = user.getString("id");
		if("error".equals(bool)){ 
			String  message = (String) result.get("message");
			throw new BizException("云财务："+message);
		} 
		SysUserEntity obj = ShiroUtils.getUserEntity();
		TPurviewCustinfoEntity  tPurviewCustinfoEntity =tPurviewCustinfoService.getinfoByUserName(obj.getUsername());
		tPurviewUsermanage.setCustid(tPurviewCustinfoEntity.getId());
		tPurviewUsermanage.setYcwid(userid);
		tPurviewUsermanage.setIsadmin("0");
		}else {
		}
		SysUserEntity obj = ShiroUtils.getUserEntity();
		tPurviewUsermanage.setCreatename(obj.getUsername());
		tPurviewUsermanage.setCreatedate(new Date());
		tPurviewUsermanage.setIsdeleted("0");
		tPurviewUsermanage.setStatus("0");
		tPurviewUsermanage.setId(UUIDUitl.getuuid());
		if(tPurviewUsermanage.getPassword()!=null){
		String password=	MD5.md5Password(tPurviewUsermanage.getPassword());
		tPurviewUsermanage.setPassword(password);
		}
		tPurviewUsermanageDao.save(tPurviewUsermanage);
		
	}
	private User changeToUser(TPurviewUsermanageEntity tPurviewUsermanage ){
		if(tPurviewUsermanage.getId()!=null){
			TPurviewUsermanageEntity	tPurviewUsermanage2=tPurviewUsermanageDao.queryObject(tPurviewUsermanage.getId());
			tPurviewUsermanage.setYcwid(tPurviewUsermanage2.getYcwid());
		}
		User user = new User();
		user.setNumber(tPurviewUsermanage.getUsername());
		user.setName(tPurviewUsermanage.getName());
		user.setPassword(tPurviewUsermanage.getPassword());
		user.setTs(new Date());
		if("1".equals(tPurviewUsermanage.getStatus())){
			user.setStatus(1);
		}else {
			user.setStatus(0);
		}
		if(!"".equals(tPurviewUsermanage.getIsdeleted())&&tPurviewUsermanage.getIsdeleted()!=null)
		user.setIsdeleted(Integer.parseInt(tPurviewUsermanage.getIsdeleted()));
		user.setId(tPurviewUsermanage.getYcwid());		
		return user; 
	}
	private String getUrlIp(){
		SysUserEntity obj = ShiroUtils.getUserEntity();
		
		TPurviewCustinfoEntity  tPurviewCustinfoEntity =tPurviewCustinfoService.getinfoByUserName(obj.getUsername());
		String ip = tPurviewCustinfoEntity.getIp()+":"+tPurviewCustinfoEntity.getServerport();
		return ip ;
	}
	@Override
	public void update(TPurviewUsermanageEntity tPurviewUsermanage){
		JSONObject  result=HttpRequestUtils.pinkUrl(getUrlIp(), "user", "update", changeToUser(tPurviewUsermanage));
	  // id 没有 更新时候  看看使用num 更新 
		String  bool = (String) result.get("status");
		if("error".equals(bool)){ 
			String  message = (String) result.get("message");
			throw new BizException("云财务："+message);
		} 
		tPurviewUsermanageDao.update(tPurviewUsermanage);
	}
	
	@Override
	public void delete(String id){
		TPurviewUsermanageEntity	tPurviewUsermanage= this.queryObject(id);
		JSONObject  result=HttpRequestUtils.pinkUrl(getUrlIp(), "user", "del", tPurviewUsermanage.getYcwid());
		String  bool = (String) result.get("status");
		if("error".equals(bool)){ 
			String  message = (String) result.get("message");
			throw new BizException("云财务："+message);
		} 
		tPurviewUsermanageDao.delete(id);
	}
	
	@Override
	public void deleteBatch(String[] ids){
		BatchIds  bat = new BatchIds();
		List<IdParam> yids = new ArrayList<IdParam>();
		bat.setIds(yids);
		for(String id : ids) {
			IdParam idParam = new IdParam();
			TPurviewUsermanageEntity tPurviewUsermanage= this.queryObject(id);
			idParam.setId(tPurviewUsermanage.getYcwid());
			yids.add(idParam);
		}
		JSONObject  result=HttpRequestUtils.pinkUrl(getUrlIp(), "user/batchdelete", "batchdelete", bat);
		String  bool = (String) result.get("status");
		if("error".equals(bool)){ 
			String  message = (String) result.get("message");
			throw new BizException("云财务："+message);
		} 
		tPurviewUsermanageDao.deleteBatch(ids);
	}

	@Override
	public List<TPurviewUsermanageEntity> queryListManage(Map<String, Object> map) {
		return tPurviewUsermanageDao.queryList(map);
	}
	
}
